﻿Imports System.Net
Imports System.Configuration
Imports System.Net.Mail


Public Class MailService

    Public Shared Sub SendMail(ByVal sendFromEmail As String, ByVal sendToEmail As String, ByVal SendMailFor As SendMailFor, ByVal body As String, ByVal MailFooter As String, Optional ByVal attachedFilePath As String = "", Optional ByVal isBodyHtml As Boolean = False)


        Dim strEmailServerIPAddress As String = Nothing
        Dim strEmailServerPort As String = Nothing
        Dim mailMessage As Mail.MailMessage = Nothing
        Dim mailClient As Mail.SmtpClient = Nothing

        If String.IsNullOrEmpty(sendFromEmail) Then sendFromEmail = ConfigurationManager.AppSettings.Get("MailFrom")

        If String.IsNullOrEmpty(sendToEmail) OrElse ConfigurationManager.AppSettings.Get("MailIsProduction").ToString.Trim = "0" Then
            body = "Mailto : " & sendToEmail & " <br> <br>" & body
            sendToEmail = ConfigurationManager.AppSettings.Get("MailToForTest")

        End If

        Dim subject As String = ""

        Select Case SendMailFor
            Case MailService.SendMailFor.RequestJoin
                subject = ConfigurationManager.AppSettings.Get("MailSubjectRequestJoin")
            Case MailService.SendMailFor.RequestBooking
                subject = ConfigurationManager.AppSettings.Get("MailSubjectRequestBooking")
            Case MailService.SendMailFor.AcceptJoin
                subject = ConfigurationManager.AppSettings.Get("MailSubjectAcceptJoin")
            Case MailService.SendMailFor.RejectJoin
                subject = ConfigurationManager.AppSettings.Get("MailSubjectRejectJoin")

            Case MailService.SendMailFor.AcceptBooking
                subject = ConfigurationManager.AppSettings.Get("MailSubjectAcceptBooking")
            Case MailService.SendMailFor.RejectBooking
                subject = ConfigurationManager.AppSettings.Get("MailSubjectRejectBooking")
        End Select

        Dim sb As New System.Text.StringBuilder()
        sb.Append("Dear  :   ทุกท่าน  <br /><br />")


        sb.Append(body)

        sb.Append("<br /><br /> จึงเรียนมาเพื่อทราบ <br />")
        sb.Append(MailFooter)

        mailMessage = New Mail.MailMessage(sendFromEmail, sendToEmail, subject, sb.ToString())
        Dim MailBCCForDev As String = ConfigurationManager.AppSettings.Get("MailBCCForDev")
        If MailBCCForDev.Trim <> "" Then mailMessage.Bcc.Add(MailBCCForDev)
        mailMessage.IsBodyHtml = isBodyHtml

        If attachedFilePath <> "" AndAlso System.IO.File.Exists(attachedFilePath) Then
            Dim at As New Mail.Attachment(attachedFilePath)
            mailMessage.Attachments.Add(at)
        End If


        strEmailServerIPAddress = ConfigurationManager.AppSettings.Get("MailServer")

        If strEmailServerIPAddress <> "" Then

            strEmailServerPort = ConfigurationManager.AppSettings.Get("MailServerPort")

            If strEmailServerPort = "" Then
                mailClient = New Mail.SmtpClient(strEmailServerIPAddress)
            Else
                mailClient = New Mail.SmtpClient(strEmailServerIPAddress, strEmailServerPort)
            End If

            Dim mailCredentialUser As String = ConfigurationManager.AppSettings("EmailServerCredentialUserName")
            Dim mailCredentialPassword As String = ConfigurationManager.AppSettings("EmailServerCredentialPassword")

            If mailCredentialUser <> "" AndAlso mailCredentialPassword <> "" Then
                mailClient.Credentials = New NetworkCredential(mailCredentialUser, mailCredentialPassword)
            End If

            'mailClient.DeliveryMethod = Mail.SmtpDeliveryMethod.Network

            Try
                mailClient.EnableSsl = True

                mailClient.Send(mailMessage)

            Catch ex As Exception
                'Throw ex
            Finally

                If mailMessage IsNot Nothing Then
                    mailMessage.Dispose()
                    mailMessage = Nothing
                End If

            End Try
        End If

    End Sub
    Public Enum SendMailFor
        RequestJoin = 1
        RequestBooking = 2
        RejectJoin = 3
        AcceptJoin = 4
        RejectBooking = 5
        AcceptBooking = 6

    End Enum
    Public Shared Sub TestSendMail()
        Dim SMTPUserName As String = "chalits"
        Dim SMTPPassword As String = "98765432"
        Dim SMTPServerName As String = "mail.bot.or.th"
        Dim MailAddress As String = "chalits@bot.or.th"
        Dim MailTo As String = "chalits@bot.or.th"

        Dim htmail As New MailMessage
        With htmail

            .From = New MailAddress(MailAddress)
            .IsBodyHtml = True
            .To.Add(MailTo)
            .Subject = ("ทดสอบส่งเมล์ Subject")
            .Body = ("ทดสอบส่งเมล์")
        End With

        Dim SMTPServer As New Net.Mail.SmtpClient(SMTPServerName)
        SMTPServer.Credentials = New System.Net.NetworkCredential(SMTPUserName, SMTPPassword)
        SMTPServer.EnableSsl = False
        SMTPServer.Port = 587
        SMTPServer.Send(htmail)
    End Sub

End Class


'Private Sub SendMail(ByVal SendMailFor As SendMailFor, ByVal v_strBody As String)

'    Try
'        v_strSubject = v_strSubject.Trim.Replace("'", """").Replace("BR", "<BR>").Replace("TAB", "&nbsp;&nbsp;&nbsp;&nbsp;")
'        v_strBody = v_strBody.Trim.Replace("'", """").Replace("BR", "<BR>").Replace("TAB", "&nbsp;&nbsp;&nbsp;&nbsp;")
'        v_strBody = "<html><body>" & v_strBody & "</body></html>"

'        v_objMailMessage.From = New MailAddress(EMailSender)
'        v_objMailMessage.Subject = v_strSubject
'        v_objMailMessage.Body = v_strBody
'        v_objMailMessage.IsBodyHtml = True
'        v_objMailMessage.SubjectEncoding = Encoding.GetEncoding("windows-874")
'        v_objMailMessage.BodyEncoding = Encoding.GetEncoding("windows-874")

'        Dim objSMTPClient As New SmtpClient
'        Dim objNetworkCredential As New NetworkCredential(EmailUserName, EMailPassword)
'        objSMTPClient.Credentials = objNetworkCredential
'        objSMTPClient.Host = SMTPServer
'        objSMTPClient.Port = SMTPServerPort
'        objSMTPClient.Send(v_objMailMessage)
'    Catch ex As Exception
'        Throw ex
'    End Try
'End Sub

